<?php
/**
 * Description...
 *
 * @api Admin
 * @author {author}<example@demo.com>
 * @version 1.0.0
 */

namespace Project\Admin\Controllers;

use Project\Admin\Controllers\BaseController;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Project\Admin\Traits\menu;

class MenuController extends BaseController
{
    use menu;

    /**
     * @api {GET} /api/admin/menu 数据列表
     * 
     * @apiDescription ...
     * 
     * @apiName  index
     * @apiGroup Admin/Menu
     * 
     * @apiParam {mixed} params ...
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function index() {
        $menus = \Project\Admin\Models\AdminMenu::get();
        $data = data_tree($menus);
        return $this->success($data);
    }

    /**
     * @api {GET} /api/admin/menu/{id} 数据详情
     * 
     * @apiDescription ...
     * 
     * @apiName  show
     * @apiGroup Admin/Menu
     * 
     * @apiParam {mixed} params ...
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function show($id) {

    }

    /**
     * @api {POST} /api/admin/menu 新增数据
     * 
     * @apiDescription ...
     * 
     * @apiName  store
     * @apiGroup Admin/Menu
     * 
     * @apiParam {mixed} params ...
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function store() {
        $fields = [
            'parent_id',
            'name',
            'icon',
            'url',
            'order',
            'component',
            'hidden',
            'menu_type',
        ];
        $params = request()->only($fields);
        $rules = [
            'parent_id' => 'sometimes|numeric',
            'name'      => 'bail|required',
            'url'       => 'bail|required|unique:admin_menus',
            'hidden'    => 'sometimes|in:yes,no',
            'menu_type' => 'sometimes|in:addtabs,dialog,ajax,blank',
        ];
        $messages = [
            'parent_id.numeric' => '上级ID数据异常.',
            'name.required'      => '菜单名不能为空.',
            'url.required'       => '菜单URL不能为空.',
        ];
        $payload = $this->requestValidator($params, $rules, $messages);

        $payload['hidden'] = 'no';
        request()->filled('hidden') && $payload['hidden'] = request()->hidden;

        $payload['menu_type'] = 'addtabs';
        request()->filled('menu_type') && $payload['menu_type'] = request()->menu_type;

        $payload['url'] = Str::start($payload['url'], '/');
        $payload['component'] = (strtolower($payload['component']) === 'layout' || !request()->filled('component') || !request()->component) ? 'layout' : Str::start($payload['component'], '/');
        \Project\Admin\Models\AdminMenu::create($payload);
        return $this->success('添加菜单成功');
    }

    /**
     * @api {PUT} /api/admin/menu/{id} 更新数据
     * 
     * @apiDescription ...
     * 
     * @apiName  update
     * @apiGroup Admin/Menu
     * 
     * @apiParam {Integer} id 数据 ID
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function update($id) {
        $fields = [
            'parent_id',
            'name',
            'icon',
            'url',
            'order',
            'component',
            'hidden',
            'menu_type',
        ];
        $params = request()->only($fields);
        $rules = [
            'parent_id' => ['sometimes', 'numeric'],
            'name'      => ['bail', 'required'],
            'url'       => ['bail', 'required', Rule::unique('\Project\Admin\Models\AdminMenu')->ignore($id)],
            'hidden'    => ['sometimes', 'in:yes,no'],
            'menu_type' => ['sometimes', 'in:addtabs,dialog,ajax,blank'],
        ];
        $messages = [
            'parent_id.numeric' => '上级菜单ID数据异常.',
            'name.required'     => '菜单名不能为空.',
            'url.required'      => '菜单URL不能为空.',
            'url.unique'        => '菜单URL已存在.',
        ];
        $payload = $this->requestValidator($params, $rules, $messages);

        $payload['hidden'] = 'no';
        request()->filled('hidden') && $payload['hidden'] = request()->hidden;

        $payload['menu_type'] = 'addtabs';
        request()->filled('menu_type') && $payload['menu_type'] = request()->menu_type;

        $payload['url'] = Str::start($payload['url'], '/');
        $payload['component'] = (strtolower($payload['component']) === 'layout' || !request()->filled('component') || !request()->component) ? 'layout' : Str::start($payload['component'], '/');
        \Project\Admin\Models\AdminMenu::where('id', $id)->update($payload);
        return $this->success('编辑菜单成功');
    }

    /**
     * @api {DELETE} /api/admin/menu/{id} 删除数据
     * 
     * @apiDescription ...
     * 
     * @apiName  destroy
     * @apiGroup Admin/Menu
     * 
     * @apiParam {Integer} id 数据 ID
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function destroy($id) {
        $menu = $this->menu_select($id);
        if (!$menu) return $this->success($menu, '菜单删除成功.');
        $menuID = array_key_get([$menu], 'id');
        if (count($menuID) > 0) {
            \Project\Admin\Models\AdminMenu::whereIn('id', $menuID)->delete();
            $this->success($menuID, '菜单删除成功.');
        }
        return $this->fail('菜单删除失败.');
    }

    /**
     * @api {GET} /api/admin/menu 创建数据页面
     * 
     * @apiDescription ...
     * 
     * @apiName  create
     * @apiGroup Admin/Menu
     * 
     * @apiParam {mixed} params ...
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function create() {

    }

    /**
     * @api {GET} /api/admin/menu/{id} 修改数据页面
     * 
     * @apiDescription ...
     * 
     * @apiName  edit
     * @apiGroup Admin/Menu
     * 
     * @apiParam {Integer} id 数据 ID
     * 
     * @apiSuccessExample {JSON} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "status_code": 200,
     *          "data" : []
     *     }
     */
    public function edit($id) {

    }

}